import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { fileURLToPath, URL } from 'node:url'
import { internalIpV4 } from 'internal-ip'
import copy from 'rollup-plugin-copy'

export default defineConfig(async () => ({
  plugins: [
    vue(),
    copy({
      targets: [
        { src: 'src/splash/splash.html', dest: 'dist' }
      ],
      hook: 'writeBundle'
    })
  ],
  // 配置 Tauri API 为外部模块
  optimizeDeps: {
    include: ['@tauri-apps/api']
  },
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  server: {
    port: 3000, // 使用固定端口
    open: true,
    host: true, // 监听所有地址，包括局域网和公网地址
    strictPort: true, // 如果端口已被占用，则中止
    hmr: {
      clientPort: 3000 // 确保客户端使用相同的端口
    }
  },
  build: {
    outDir: 'dist',
    assetsDir: 'assets',
    rollupOptions: {
      output: {
        format: 'es'
      },
      external: ['@tauri-apps/api']
    }
  },
  // Vite options tailored for Tauri development and only applied in `tauri dev` or `tauri build`
  clearScreen: false,
  // tauri expects a fixed port, fail if that port is not available
  envPrefix: ['VITE_', 'TAURI_'],
  // prevent vite from obscuring rust errors
  // to make use of `TAURI_DEBUG` and other env variables
  // https://tauri.studio/v1/api/config#buildconfig.beforedevcommand
}))